/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.controller.user;

import akka.actor.Actor;
import akka.actor.ActorRef;
import com.cxhd.pacs.DataConfig;
import com.cxhd.pacs.RegisterPlugin;
import com.cxhd.pacs.actor.ActorManager;
import com.cxhd.pacs.controller.service.HostService2;
import com.cxhd.pacs.interceptor.HostInfoInterceptor;
import com.cxhd.pacs.model.CacheData;
import com.cxhd.pacs.model.FileDetail;
import com.cxhd.pacs.model.FileSystem;
import com.cxhd.pacs.model.HostDetail;
import com.cxhd.pacs.model.HostInfo;
import com.cxhd.pacs.model.LisDetail;
import com.cxhd.pacs.plugins.LisDataPlugin;
import com.cxhd.pacs.services.HostService;
import com.cxhd.pacs.tools.DateUtil;
import com.cxhd.pacs.tools.FileUtil;
import com.cxhd.pacs.tools.HttpClient;
import com.cxhd.pacs.tools.ImageRender;
import com.cxhd.pacs.tools.LisXml;
import com.cxhd.pacs.tools.PDFRender;
import com.cxhd.pacs.tools.PinYinUtil;
import com.cxhd.pacs.tools.QrCodeRender;
import com.cxhd.pacs.tools.ZipFileTool;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.zeroturnaround.zip.ZipUtil;

/**
 *
 * @author Administrator
 */
public class HostCtrl extends Controller {

    HostInfo hi = new HostInfo();
    HostDetail hd = new HostDetail();
    private String url = "http://base.tronsystem.com:19027/app/message/sendOtherMessage.data";
    private ActorRef arf = ActorManager.getIntance().getActor("savePatInfoActor");

    Gson gson = new Gson();

    public void index() {
        setSessionAttr("hospitalName", getPara("hospitalName"));
        if (getPara("p", null) != null) {
            setAttr("p", getPara("p"));
        }
        if (DataConfig.getInstance().getConfig().get("system_mode") != null && DataConfig.getInstance().getConfig().get("system_mode").equalsIgnoreCase("all")) {
            render("/patient/list_1.jsp");
        } else {
            if (getPara("type") != null) {
                render("/patient/list.jsp");
            } else {
                redirect("/main");
            }
        }
    }

    public void all() {

        render("/patient/list_1.jsp");

    }

    public void patient_pacs() {
        render("/patient/list_pacs.jsp");
    }

    public void patient_us() {
        render("/patient/list_us.jsp");
    }

    public void patient_ecg() {
        render("/patient/list_ecg.jsp");
    }

    public void patient_lis() {
        render("/patient/list_lis.jsp");
    }

    public void patient_emr() {
        render("/patient/list_emr.jsp");
    }

    public void patient_dm() {
        render("/patient/list_dm.jsp");
    }

//    @Before(HostInfoInterceptor.class)
    public void getHost() {

        renderText(new HostInfo().getHostId(this));
    }

//    public void getHostList() {
//
//        String sql = "select * ";
//        String serarchContent = getPara("content", null);
//        String param = "from hostinfo order by last_event_time desc";
//        if (serarchContent != null) {
//            param = "from hostinfo where host_id like '%" + serarchContent + "%' or name like '%" + serarchContent + "%'  or phone like '%" + serarchContent + "%'  or card_no like '%" + serarchContent + "%' or pym like '%" + serarchContent + "%' order by last_event_time";
//
//        }
//
//        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);
//
//        renderJson(userPage);
//    }
    public void getHostList() {

        String sql = "select b.*,c.name as hos_name ";
        String serarchContent = getPara("content", null);
        String searchHospitalCode = getPara("hospital");
        String search1 = "";
        if (searchHospitalCode != null && !searchHospitalCode.equalsIgnoreCase("")) {
            search1 = "where and b.hospital_code='" + searchHospitalCode + "'";
            System.out.println(search1);
        }
        if (getPara("start_date") != null) {

            if (search1.equalsIgnoreCase("")) {
                search1 = "where and a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            } else {
                search1 += " a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            }

        }
        System.out.println(search1);
        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
        String param = "from  (select host_id,hospital_code,info_id from host_detail GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " ";

// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
        if (serarchContent != null) {

            param = "from  (select host_id,hospital_code,info_id from host_detail GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " ";
            //param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by a.host_id  order by a.host_id desc";

            //param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by b.host_id order by b.host_id desc";
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }
//    public void getHostList() {
//
//        String sql = "select b.*,c.name as hos_name ";
//        String serarchContent = getPara("content", null);
//        String searchHospitalCode = getPara("hospital");
//        String search1 = "";
//        if (searchHospitalCode != null && !searchHospitalCode.equalsIgnoreCase("")) {
//            search1 = "where and b.hospital_code='" + searchHospitalCode + "'";
//            System.out.println(search1);
//        }
//        if (getPara("start_date") != null) {
//
//            if (search1.equalsIgnoreCase("")) {
//                search1 = "where and b.last_event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
//            } else {
//                search1 += " b.last_event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
//            }
//
//        }
//        System.out.println(search1);
//        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
//        String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " order by last_event_time desc ";
//
//// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
//        if (serarchContent != null) {
//
//            param = "from  hostinfo b  left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " order by last_event_time desc";
//        }
//        System.out.println(sql + param);
//        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);
//
//        renderJson(userPage);
//    }

    public void getHostListByPacs() {

        String sql = "select b.*,c.name as hos_name ,event_name,event_time ";
        String serarchContent = getPara("content", null);
        String searchHospitalCode = getPara("hospital");
        String search1 = "";
        if (searchHospitalCode != null && !searchHospitalCode.equalsIgnoreCase("")) {
            search1 = "where and b.hospital_code='" + searchHospitalCode + "'";
            System.out.println(search1);
        }
        if (getPara("start_date") != null) {

            if (search1.equalsIgnoreCase("")) {
                search1 = "where and a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            } else {
                search1 += " a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            }

        }
        System.out.println(search1);
        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
        String param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name in('DX','CT','CR','MRI') GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " ";

// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
        if (serarchContent != null) {

            param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name in('DX','CT','CR','MRI') GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " ";
            //param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by a.host_id  order by a.host_id desc";

            //param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by b.host_id order by b.host_id desc";
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void getHostListByUs() {

        String sql = "select b.*,c.name as hos_name,event_name,event_time  ";
        String serarchContent = getPara("content", null);
        String searchHospitalCode = getPara("hospital");
        String search1 = "";
        if (searchHospitalCode != null && !searchHospitalCode.equalsIgnoreCase("")) {
            search1 = "where and b.hospital_code='" + searchHospitalCode + "'";
            System.out.println(search1);
        }
        if (getPara("start_date") != null) {

            if (search1.equalsIgnoreCase("")) {
                search1 = "where and a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            } else {
                search1 += " a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            }

        }
        System.out.println(search1);
        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
        String param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='US' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " ";

// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
        if (serarchContent != null) {

            param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='US' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " ";
            //param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by a.host_id  order by a.host_id desc";

            //param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by b.host_id order by b.host_id desc";
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void getHostListByDm() {

        String sql = "select b.*,c.name as hos_name,event_name,event_time  ";
        String serarchContent = getPara("content", null);
        String searchHospitalCode = getPara("hospital");
        String search1 = "";
        if (searchHospitalCode != null && !searchHospitalCode.equalsIgnoreCase("")) {
            search1 = "where and b.hospital_code='" + searchHospitalCode + "'";
            System.out.println(search1);
        }
        if (getPara("start_date") != null) {

            if (search1.equalsIgnoreCase("")) {
                search1 = "where and a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            } else {
                search1 += " a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            }

        }
        System.out.println(search1);
        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
        String param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='DM' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " ";

// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
        if (serarchContent != null) {

            param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='DM' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " ";
            //param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by a.host_id  order by a.host_id desc";

            //param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by b.host_id order by b.host_id desc";
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void getHostListByEcg() {

        String sql = "select b.*,c.name as hos_name,event_name,event_time  ";
        String serarchContent = getPara("content", null);
        String searchHospitalCode = getPara("hospital");
        String search1 = "";
        if (searchHospitalCode != null && !searchHospitalCode.equalsIgnoreCase("")) {
            search1 = "where and b.hospital_code='" + searchHospitalCode + "'";
            System.out.println(search1);
        }
        if (getPara("start_date") != null) {

            if (search1.equalsIgnoreCase("")) {
                search1 = "where and a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            } else {
                search1 += " a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            }

        }
        System.out.println(search1);
        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
        String param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='ECG' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " ";

// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
        if (serarchContent != null) {

            param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='ECG' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " ";
            //param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by a.host_id  order by a.host_id desc";

            //param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by b.host_id order by b.host_id desc";
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void getHostListByLis() {

        String sql = "select b.*,c.name as hos_name,event_name,event_time  ";
        String serarchContent = getPara("content", null);
        String searchHospitalCode = getPara("hospital");
        String search1 = "";
        if (searchHospitalCode != null && !searchHospitalCode.equalsIgnoreCase("")) {
            search1 = "where and b.hospital_code='" + searchHospitalCode + "'";
            System.out.println(search1);
        }
        if (getPara("start_date") != null) {

            if (search1.equalsIgnoreCase("")) {
                search1 = "where and a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            } else {
                search1 += " a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            }

        }
        System.out.println(search1);
        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
        String param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='检验' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " ";

// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
        if (serarchContent != null) {

            param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='检验' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " ";
            //param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by a.host_id  order by a.host_id desc";

            //param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by b.host_id order by b.host_id desc";
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void getHostListByEmr() {

        String sql = "select b.*,c.name as hos_name,event_name,event_time  ";
        String serarchContent = getPara("content", null);
        String searchHospitalCode = getPara("hospital");
        String search1 = "";
        if (searchHospitalCode != null && !searchHospitalCode.equalsIgnoreCase("")) {
            search1 = "where and b.hospital_code='" + searchHospitalCode + "'";
            System.out.println(search1);
        }
        if (getPara("start_date") != null) {

            if (search1.equalsIgnoreCase("")) {
                search1 = "where and a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            } else {
                search1 += " a.event_time BETWEEN '" + getPara("start_date") + "' and '" + getPara("end_date") + "'";
            }

        }
        System.out.println(search1);
        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
        String param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name in('病历','医嘱') GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " ";

// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
        if (serarchContent != null) {

            param = "from  (select host_id,hospital_code,info_id,event_name,event_time from host_detail where event_name ='病历' GROUP BY host_id ORDER BY event_time desc) a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " ";
            //param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by a.host_id  order by a.host_id desc";

            //param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by b.host_id order by b.host_id desc";
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void patient_detail() {
        HostInfo info = hi.getPatientInfo(getPara("host_id"));
        String order_id = getPara("order_id");
        if (order_id != null) {
            Record orderInfo = Db.findFirst("select * from asklist where pk = " + order_id);
            setAttr("doctor_phone", orderInfo.get("recvicer_phone"));
            setAttr("order_pk", orderInfo.get("pk"));
        }

        setAttr("item", JsonKit.toJson(info));
        setAttr("host_id", getPara("host_id"));
        setAttr("patientName", info.get("name"));
        render("/patient/patient_detail.jsp");

    }

    public void viewForPhone() {
        HostInfo info = hi.getPatientInfo(getPara("host_id"));
        setAttr("patientName", info.get("name"));
        setAttr("item", JsonKit.toJson(info));

        setAttr("host_id", getPara("host_id"));
        render("/patient/moblieDetail.jsp");
    }

    public void paste_detail() {
        try {
            new HostDetail().pasteDetail(getPara("pk"), getPara("host_id"));
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }

    }

    public void sendNotice() {
        System.out.println("您有来自[" + (String) getSessionAttr("hospitalName") + "}的网络门诊请求！病人名：" + getPara("name") + ";性别：" + getPara("sex") + ";年龄:" + getPara("age"));
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("fromId", "06211386"));
        params.add(new BasicNameValuePair("name", "网络门诊"));
        //params.add(new BasicNameValuePair("toId", "07254262"));
        //params.add(new BasicNameValuePair("toId", "02765089"));
        params.add(new BasicNameValuePair("toId", "02611696"));
        params.add(new BasicNameValuePair("contact", "您有来自[" + (String) getSessionAttr("hospitalName") + "]的网络门诊请求！病人名：" + getPara("name") + ";性别：" + getPara("sex") + ";年龄:" + getPara("age")));
        params.add(new BasicNameValuePair("data", "http://60.161.186.217:2727/MedicalInfo/patient/openDetail?host_id=" + getPara("host_id")));
        String hostId = HttpClient.httpPost(url, params);
        System.out.println(hostId + "********************************");
        renderText("true");
    }

    public void sendNotice2() {
        String appId = getPara("appId");
        if (appId != null && appId.equalsIgnoreCase("oEI1ktwPl61Hh9lwtbk-JN3ST5fA")) {
            System.out.println("您有来自[云县人民医院]的网络申请！病人名：陈朝柱;性别：男;年龄:66");
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("fromId", "06211386"));
            params.add(new BasicNameValuePair("name", "网络门诊"));
            //params.add(new BasicNameValuePair("toId", "07254262"));
            //params.add(new BasicNameValuePair("toId", "02765089"));
            params.add(new BasicNameValuePair("toId", "02611696"));
            params.add(new BasicNameValuePair("contact", "您有来自[云县人民医院]的网络申请！病人名：陈朝柱;性别：男;年龄:66"));
            params.add(new BasicNameValuePair("data", "http://60.161.186.217:2727/MedicalInfo/patient/openDetail?host_id=" + getPara("host_id")));
            String hostId = HttpClient.httpPost(url, params);
            System.out.println(hostId + "********************************");
            renderText("true");
        } else {
            renderText("false");
        }
    }

    public void getDetails() {
        List<HostDetail> list = hd.getDetail(getPara("host_id"));
        for (HostDetail hs : list) {
//            if (hs.get("result_type").equals("html")) {
//                hs.set("event_result", "点击查看报告");
//            }
        }
        renderJson(list);
    }

    public void getHospital() {
        renderJson(Db.find("select * from hospital group by name"));
    }

    public void getCount() {
        List<Record> re = Db.find("select event_name as `name`,COUNT(*) as `value` FROM host_detail WHERE host_id = '" + getPara("host_id") + "' GROUP BY event_name");
        renderJson(re);
    }

    public void getResult() {
        if (getPara("fileId") != null && getPara("fileId").length() < 20 && getPara("type").equalsIgnoreCase("html")) {
            String fileId = getPara("fileId");
            FileDetail mo = FileDetail.dao.getFile(fileId);
            File myFile = null;
            System.out.println(mo.getStr("type"));

            myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));
            renderText(FileUtil.outWriter(myFile.getAbsolutePath()));
        } else {
            renderText(hd.getResult(getPara("pk")));
        }
    }

    public void saveInfo() {
        renderText(hd.saveDetail(this) + "");
    }

    public void getLisResult() {

        renderJson(LisDetail.dao.getDetail(getPara("pk")));

    }

    /*
    *保存报告
     */
    public void saveRemoteRpt() {
        String detail_id = hd.saveDetail(this);
        if (detail_id != null && getPara("order_id") != null) {
            Db.update("update asklist set report_pk ='" + detail_id + "',report_time='" + DateUtil.getDateEN() + "',status=2 where pk=" + getPara("order_id"));
        }
        if (detail_id != null) {
            renderText("true");
        } else {
            renderText("false");
        }

    }

    /*
    缓存报告
     */
    public void saveCacheRpt() {
        try {
            if (getPara("cacheName", null) != null) {
                new CacheData().saveCache(getPara("cacheName"), getPara("cacheValue"), getPara("hostId"));
                renderText("true");
            } else {
                renderText("false");
            }

        } catch (Exception e) {
            renderText("false");

        }
    }

    /*
    *提取缓存
     */
    public void getCacheRpt() {
        String cacheValue = new CacheData().getRptCache(getPara("hostId"), getPara("cacheName"));
        if (cacheValue != null) {
            renderText(cacheValue);
        } else {
            renderText("false");
        }
    }

    public void test() {
        renderText(getPara("name"));
    }

    public void deleteItem() {
        renderText(hd.deleteItem(getPara("pk")));
    }

    public void deletePatient() {
        renderText(new HostInfo().deletePatient(getPara("pk")));
    }

    public void uploadData() {
        try {
            String fileName = getPara("fileName");

            FileSystem bf = FileSystem.dao.getPathInfo();

            int fileSystemId = 0;
            String dir = "";
            String baseDir = "";
            String type = "file";
            if (bf != null) {
                baseDir = bf.get("base_path");
                dir = bf.get("base_path");
                fileSystemId = bf.getInt("pk");
            }

            File fs = new File(dir + "/" + DateUtil.getNumFolder());
            if (!fs.exists()) {
                fs.mkdirs();
            }
            String filePath = new Date().getTime() + "";
            String dateFolder = DateUtil.getNumFolder();
            fs = new File(dir + "/" + dateFolder + filePath);
            filePath = dateFolder + filePath;
            BufferedInputStream bis = null;
            try {
                bis = new BufferedInputStream(getRequest().getInputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(fs));
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
            byte[] b = new byte[1024 * 64];
            int len = 0;
            while ((len = bis.read(b)) > 0) {
                bos.write(b, 0, len);
            }
            bos.flush();
            bos.close();

            String result = new FileDetail().saveInfo(type, fileName, filePath, fs.length() + "", fileSystemId + "");
            //   }
            renderText(result);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    //上传压缩文件解压并解析
    public void uploadDataZip() {
        try {
            String hospitalCode = getPara("hospitalCode");
            if (hospitalCode == null || !hospitalCode.startsWith("CX0712131")) {
                hospitalCode = DataConfig.getInstance().getConfig().get("centerHospitalCode");
            }
            FileSystem bf = FileSystem.dao.getPathInfo();

            int fileSystemId = 0;
            String dir = "";
            String baseDir = "";
            String type = "file";
            if (bf != null) {
                baseDir = bf.get("base_path");
                dir = bf.get("base_path");
                fileSystemId = bf.getInt("pk");
            }
            String filePath = new Date().getTime() + "";
            File fs = new File(dir + "/" + DateUtil.getNumFolder() + filePath);
            if (!fs.exists()) {
                fs.mkdirs();
            }

            fs = new File(dir + "/" + DateUtil.getNumFolder() + filePath + "/" + filePath + ".zip");

            BufferedInputStream bis = null;
            try {
                bis = new BufferedInputStream(getRequest().getInputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(fs));
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
            byte[] b = new byte[1024 * 64];
            int len = 0;
            while ((len = bis.read(b)) > 0) {
                bos.write(b, 0, len);
            }
            bos.flush();
            bos.close();

            //解压文件
            ZipUtil.explode(new File(fs.getAbsolutePath()));
            //FileUtil.getAllFile(filePath);
            new HostService().parsreJsonAndSave(FileUtil.getAllFile(fs.getAbsolutePath()), fileSystemId + "", this);
            // String result = new FileDetail().saveInfo(type, fileName, filePath, fs.length() + "", fileSystemId + "");
            //   }
            renderText("true");

        } catch (Exception ex) {
            ex.printStackTrace();
            renderText("false");
        }

    }

    public void getImage() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new ImageRender(myFile, ""));

    }

    public void getPdf() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new PDFRender(myFile, ""));

    }

    public void getVideo() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new PDFRender(myFile, ""));

    }

    public void saveLotInfo() {
        String host_id = getPara("host_id");
        String data = getPara("data");
        try {
            if (data != null) {
                List<Map<String, String>> list = gson.fromJson(data, new TypeToken<List<Map<String, String>>>() {
                }.getType());
                for (Map<String, String> map : list) {
                    map.put("host_id", host_id);
                    new HostDetail().saveDetail(map);
                }
                renderText("true");
            } else {
                renderText("false");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }

    public void lisDetail() {
        String patientInfo = null, result = null;
        try {
            patientInfo = getPara("patient_info");
            result = getPara("result");
        } catch (Exception ex) {

        }

        String hostId = null;
        String detail_id = null;
        if (patientInfo != null) {
            try {
                Map<String, String> pi = LisXml.parsePatientXml(patientInfo);
                String sample_name = Db.findFirst("select * from ut_dict_report where code=?", pi.get("sample_no").substring(0, 2)).getStr("report_name");
                Map<String, String> info = new HashMap<String, String>();
                if (!pi.isEmpty()) {
                    info.put("name", pi.get("patient_name"));
                    info.put("age", pi.get("patient_age"));
                    info.put("birth", pi.get("patient_birth"));
                    info.put("card_no", pi.get("card_no"));
                    info.put("sex", pi.get("patient_sex").equalsIgnoreCase("1") ? "男" : "女");
                    info.put("address", pi.get("pat_address"));
                    info.put("phone", pi.get("pat_phone"));
                    hostId = new HostInfo().getHostId(info);
                }
                if (hostId != null) {
                    Map<String, String> detail = new HashMap<String, String>();
                    detail.put("host_id", hostId);
                    detail.put("department", pi.get("office"));
                    detail.put("event_name", "检验");
                    detail.put("update_id", getPara("update_id"));
                    detail.put("hospital_name", DataConfig.getInstance().getHospitalName());
                    try {
                        detail.put("event_description", sample_name);
                    } catch (Exception e) {

                        e.printStackTrace();
                    }
                    detail.put("data_type", "lis_detail");
                    detail.put("result_type", "table");
                    detail_id = hd.saveDetail2(detail);
                }

                if (detail_id != null && result != null) {
                    List<Map<String, String>> list = LisXml.parseResultXml(result);

                    for (Map<String, String> map : list) {
                        map.put("detail_id", detail_id);
                        new LisDetail().saveInfo(map);
                    }
                    //调用插件
                    try {
                        if (DataConfig.getInstance().checkPlugin("plugin_lis_data")) {
                            ((LisDataPlugin) RegisterPlugin.getInstance().getPlugin("plugin_lis_data")).setData(list, pi.get("patient_code"), sample_name, pi.get("patient_name"), pi.get("patient_age"), pi.get("patient_sex").equalsIgnoreCase("1") ? "男" : "女").start();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                renderText("true");
            } catch (Exception e) {
                e.printStackTrace();
                renderText("false");
            }

        } else {
            renderText("false");
        }
    }

    //接收LIS 系统数据
    public void lisDetailByCxLis() {
        Map<String, String> pi = null;
        List<Map<String, String>> result = null;
        String hospital_code = getPara("hospital_code");
        if (hospital_code == null || !hospital_code.startsWith("CX0712131")) {
            hospital_code = DataConfig.getInstance().getConfig().get("centerHospitalCode");
        }
        try {
            pi = gson.fromJson(getPara("patientInfo"), new TypeToken<Map<String, String>>() {
            }.getType());
            result = gson.fromJson(getPara("result"), new TypeToken<List<Map<String, String>>>() {
            }.getType());
        } catch (Exception ex) {

        }

        String hostId = null;
        String detail_id = null;
        if (pi != null) {
            try {
                String sample_name = null;
                if (pi.get("event_description") != null) {
                    sample_name = pi.get("event_description");
                } else {
                    sample_name = Db.findFirst("select * from ut_dict_report where code=?", pi.get("sample_no").substring(0, 2)).getStr("report_name");
                }
                Map<String, String> info = new HashMap<String, String>();
                if (!pi.isEmpty()) {
                    info.put("name", pi.get("name"));
                    info.put("age", pi.get("age"));
                    info.put("birth", pi.get("birth"));
                    info.put("card_no", pi.get("card_no"));
                    info.put("sex", pi.get("sex"));
                    info.put("address", pi.get("address"));
                    info.put("phone", pi.get("phone"));
                    info.put("info_id", pi.get("info_id"));
                    info.put("hospital_code", hospital_code);
                    hostId = new HostInfo().getHostId(info);
                }
                if (hostId != null) {
                    Map<String, String> detail = new HashMap<String, String>();
                    detail.put("host_id", hostId);
                    detail.put("department", pi.get("department"));
                    detail.put("event_name", "检验");
                    detail.put("event_time", pi.get("datetime"));
                    detail.put("update_id", pi.get("update_id"));
                    detail.put("info_id", pi.get("info_id"));
                    detail.put("hospital_code", hospital_code);
                    try {
                        detail.put("event_description", sample_name);
                    } catch (Exception e) {

                        e.printStackTrace();
                    }
                    detail.put("data_type", "lis_detail");
                    detail.put("result_type", "table");

                    if (hd.checkUpdateId(pi.get("update_id"), hostId)) {

                        detail_id = hd.saveDetail2(detail);
                    } else {
                        detail_id = hd.updateDetail(detail);
                        //删除LISdetail 表的数据
                        Db.update("delete from lis_detail where detail_id='" + detail_id + "'");
                    }

                    info.putAll(detail);

                }

                if (detail_id != null && result != null) {
                    info.put("detail_id", detail_id);
                    arf.tell(info, ActorRef.noSender());
                    for (Map<String, String> map : result) {
                        map.put("detail_id", detail_id);
                        if (map.get("text_range") == null) {
                            if (map.get("low") != null) {
                                map.put("text_range", map.get("low") + "~" + map.get("high"));
                                map.remove("low");
                                map.remove("high");
                            } else {
                                map.put("text_range", "");
                            }
                        }

                        new LisDetail().saveInfo(map);
                    }

                }
                renderText("true");
            } catch (Exception e) {
                e.printStackTrace();
                renderText("false");
            }

        } else {
            renderText("false");
        }

    }

    public void checkUpdateId() {
        // renderText("" + hd.checkUpdateId("222222222"));
    }

    public void addPacsInfo() {
        try {
            String hospital_code = getPara("hospital_code");
            if (hospital_code == null || !hospital_code.startsWith("CX0712131")) {
                hospital_code = DataConfig.getInstance().getConfig().get("centerHospitalCode");
            }
            String event_time = getPara("event_time");
            if (event_time == null || event_time.equalsIgnoreCase("null")) {
                event_time = DateUtil.getDateEN();
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", getPara("name"));
            map.put("sex", getPara("sex"));
            if (getPara("age") != null) {
                map.put("age", getPara("age").trim());
            }
            map.put("birth", getPara("birth"));
            map.put("address", getPara("address"));
            map.put("phone", getPara("phone"));
            map.put("card_no", getPara("card_no"));
            map.put("host_id", getPara("host_id"));
            map.put("info_id", getPara("info_id"));//
            map.put("update_id", getPara("update_id"));// studyiuid
            map.put("hospital_code", hospital_code);

            String host_id = new HostInfo().getHostId(map);
            Map<String, String> map_detail = new HashMap<String, String>();
            if (host_id != null) {
                map_detail.put("host_id", host_id);
                map_detail.put("event_name", getPara("event_name"));
                map_detail.put("department", getPara("department"));
                map_detail.put("event_description", getPara("event_description"));
                map_detail.put("event_result", getPara("event_result"));
                map_detail.put("event_time", event_time);
                map_detail.put("doctor", getPara("doctor"));
                map_detail.put("hospital_code", hospital_code);
                map_detail.put("result_type", "html");
                map_detail.put("bed_no", getPara("bed_no"));
                map_detail.put("info_id", getPara("info_id"));
                map_detail.put("update_id", getPara("update_id"));
                //System.out.println(hd.checkUpdateId(getPara("update_id"), host_id));
                String fileName = new Date().getTime() + "";
                if (getPara("update_id") != null) {
                    fileName = getPara("update_id");
                }
                if (hd.checkUpdateId(getPara("update_id"), host_id)) {
                    map_detail.put("event_result", new HostService2().saveHhtml(fileName, map_detail.get("event_result")));
                    hd.saveDetail2(map_detail);
                } else {
                    map_detail.put("event_result", new HostService2().saveHhtml(fileName, map_detail.get("event_result")));

                    hd.updateDetail(map_detail);
                }
                map.putAll(map_detail);
                arf.tell(map, ActorRef.noSender());
                renderText("true");
            }
        } catch (Exception e) {

            e.printStackTrace();
            renderText("false");
        }

    }

    public void addInfo() {
        try {
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", getPara("name"));
            map.put("sex", getPara("sex"));
            map.put("age", getPara("age").trim());
            map.put("birth", getPara("birth"));
            map.put("address", getPara("address"));
            map.put("phone", getPara("phone"));
            map.put("card_no", getPara("card_no"));
            map.put("host_id", getPara("host_id"));
            String host_id = new HostInfo().getHostId(map);
            Map<String, String> map_detail = new HashMap<String, String>();
            if (host_id != null) {
                map_detail.put("host_id", host_id);
                map_detail.put("event_name", getPara("event_name"));
                map_detail.put("department", getPara("department"));
                map_detail.put("event_description", getPara("event_description"));
                map_detail.put("event_result", getPara("event_result"));
                map_detail.put("event_time", getPara("event_time"));
                map_detail.put("doctor", getPara("doctor"));
                map_detail.put("hospital_name", DataConfig.getInstance().getHospitalName());
                map_detail.put("result_type", "json");
                map_detail.put("bed_no", getPara("bed_no"));
                map_detail.put("update_id", getPara("update_id"));
                System.out.println(hd.checkUpdateId(getPara("update_id"), host_id));
                if (hd.checkUpdateId(getPara("update_id"), host_id)) {

                    hd.saveDetail2(map_detail);
                } else {
                    hd.updateDetail(map_detail);
                }
                renderText("true");
            }
        } catch (Exception e) {

            e.printStackTrace();
            renderText("false");
        }

    }

    public void addPY() {
        List<Record> list = Db.find("select * from hostinfo where pym is null ");
        for (Record re : list) {
            String pym = PinYinUtil.getPinYin(re.get("name") + "");
            Db.update("update hostinfo set pym='" + pym + "' where pk=" + re.get("pk"));
        }
    }

    public void openDetail() {
        HostInfo info = hi.getPatientInfo(getPara("host_id"));
        setAttr("time_id", getPara("date"));
        setAttr("item", JsonKit.toJson(info));

        setAttr("patientName", info.getStr("name"));

//        List<HostDetail> list = hd.getDetail(getPara("host_id"));
//        for (HostDetail hs : list) {
//            if (hs.get("result_type").equals("html")) {
//                hs.set("event_result", "点击查看报告");
//            }
//        }
        setAttr("host_id", getPara("host_id"));
        setAttr("flag", getPara("flag"));
        render("/patient/patient_detail.jsp");

    }

    public void openPatientMedicalInfo() {
        try {
            HostInfo info = hi.getPatientInfo(getPara("pat_id"), getPara("hospital_code"));
            // HostInfo info = hi.getPatientInfo("2016006188","DESKTOP-VBVFATK");
            // setAttr("time_id", getPara("date"));
            setAttr("item", JsonKit.toJson(info));

            setAttr("patientName", info.getStr("name"));

//        List<HostDetail> list = hd.getDetail(getPara("host_id"));
//        for (HostDetail hs : list) {
//            if (hs.get("result_type").equals("html")) {
//                hs.set("event_result", "点击查看报告");
//            }
//        }
            setAttr("host_id", info.getStr("host_id"));
            //setAttr("flag", getPara("flag"));
            render("/patient/patient_detail.jsp");
        } catch (Exception e) {
            renderText("您所查找的资料没有找到，请确认病人资料已经上传到平台");
        }

    }

    public void openRpt() {
        Record re = Db.findFirst("select * from hostinfo where host_id='" + getPara("host_id") + "'");
        System.out.println("select * from hostinfo where host_id='" + getPara("host_id") + "'");

        setAttr("pat_info", re.toJson());
        setAttr("order_id", getPara("order_id"));
        render("/patient/remoteRpt.jsp");
    }

    public void readRpt() {

        setAttr("report_pk", getPara("report_pk"));
        render("/patient/remoteRptRead.jsp");
    }

    public void getRpt() {
        Record re = Db.findFirst("select * from host_detail where pk =" + getPara("report_pk"));
        renderText(re.getStr("event_result"));
    }

    public void getQrCode() {
        render(new QrCodeRender(getPara("data")));
    }

    public void openResult() {
        setAttr("result_pk", getPara("result_pk"));
        render("/patient/resultPage.jsp");
    }

    public void getResultItem() {
        Record re = Db.findFirst("select * from host_detail where pk =" + getPara("result_pk"));
        renderJson(re);
    }

    public void getMobileDetail() {
        renderJson(Db.find("select * from host_detail where host_id = '" + getPara("host_id") + "' order by host_id desc "));
    }

    public void getMobileDetailFilter() {
        renderJson(Db.find("select * from host_detail where host_id = '" + getPara("host_id") + "' and event_name='" + getPara("event_name") + "' order by event_time desc "));
    }

    public void updateHtmlData() {

        List<Record> list = Db.find("select * from host_detail where result_type ='html' and LENGTH(event_result)>20 limit 1000 ");
        for (Record re : list) {
            try {
                if (re.getStr("event_result").length() > 20) {
                    String fileName = new Date().getTime() + "";
                    if (re.get("update_id") != null) {
                        fileName = re.get("update_id") + "";
                    }
                    String result = new HostService2().saveHhtml(fileName, re.getStr("event_result"));
                    Db.update("update host_detail set event_result='" + result + "' where pk ='" + re.get("pk") + "'");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        renderText("true");

    }

    public void getPdfToImage() {
        String fileId = getPara("fileId");
        FileDetail fd = new FileDetail().getFile(fileId);
        String path = fd.get("base_path") + "/" + fd.get("path") + "";
        System.out.println(path);
        File[] fl = new File(path).getParentFile().listFiles(new FileFilter() {

            public boolean accept(File f) {
                if (f.getName().endsWith(".jpg")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        List<String> listf = new ArrayList<String>();
        if (fl != null && fl.length > 0) {
            for (File f : fl) {
                listf.add(f.getAbsolutePath());
            }
        }
        renderJson(listf);
    }

    public void getPdfImage() {
        render(new ImageRender(new File(getPara("path")), null));
    }

    public void openApply() {

        redirect("/main/view/ask/index", true);
    }

    public void viewApply() {
        redirect("/main/view/ask/viewApply", true);
    }

}
